package mblog.core.persist.dao.impl;

import mblog.core.persist.dao.MailDao;
import mblog.core.persist.entity.MailPO;
import mtons.modules.annotation.Repository;
import mtons.modules.persist.impl.BaseRepositoryImpl;
import mtons.modules.pojos.Paging;
import org.hibernate.Query;

import java.util.List;

/**
 * Project  : mblog
 * Author   : Wu Tian Qiang
 * Date     : 2016/7/13
 */
@Repository(entity = MailPO.class)
public class MailDaoImpl extends BaseRepositoryImpl<MailPO> implements MailDao {
    @Override
    public List<MailPO> paging(Paging paging) {
        PagingQuery<MailPO> q = pagingQuery(paging);
        q.desc("id");
        return q.list();
    }

    @Override
    public List<MailPO> findMail(Integer amount) {
        PagingQuery<MailPO> q = pagingQuery(new Paging(amount));
        q.asc("sendAmount");
        return q.list();
    }

    @Override
    public void updateSendAmount(String mail) {
        Query query = this.createQuery("update MailPO mp set mp.sendAmount = mp.sendAmount + 1 where mp.mail = :mail");
        query.setParameter("mail", mail);
        query.executeUpdate();
    }
}
